package leetcode.code200;

public class Solution extends Solution200 {

	private int m;// 岛宽
	private int n;// 岛长

	@Override
	public int numIslands(char[][] grid) {
		int numoflands = 0;
		m = grid.length;
		n = grid[0].length;// 岛是规则的长方形
		for (int i = 0; i < m; i++) {
			for (int j = 0; j < n; j++) {
				if (grid[i][j] == '1') {
					numoflands++;
					this.Infected(grid, i, j);
				}
			}
		}
		return numoflands;
	}

	private void Infected(char[][] grid, int i, int j) {
		if (i >= 0 && i < m && j >= 0 && j < n && grid[i][j] == '1') {
			grid[i][j] = '2';
			this.Infected(grid, i + 1, j);
			this.Infected(grid, i - 1, j);
			this.Infected(grid, i, j + 1);
			this.Infected(grid, i, j - 1);
		}
	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();

	}

}
